package com.talkyun.openx.codec;

import com.talkyun.openx.client.codec.JsonCodec;
import com.talkyun.openx.codec.model.PageResult;
import com.talkyun.utils.json.JSON;
import junit.framework.Assert;
import junit.framework.TestCase;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;

public class JsonCodecTestCase extends TestCase {

    private JsonCodec codec = new JsonCodec();

    public PageResult<PageResult.Item> pageResultMethod() {
        return null;
    }

    public void testCodecEncode() {
        codec.decode(Date.class, "");
    }

    public void testPageGenericType() throws NoSuchMethodException {
        PageResult<PageResult.Item> page = new PageResult<PageResult.Item>();

        ArrayList<PageResult.Item> result = new ArrayList<PageResult.Item>();
        result.add(new PageResult.Item("name", 120));
        result.add(new PageResult.Item("name2", 20));

        page.setResult(result);
        page.setPageSize(25);

        String json = JSON.toJsonString(page);

        System.out.println(json);

        Method method = this.getClass().getMethod("pageResultMethod");

        @SuppressWarnings("unchecked")
        PageResult<PageResult.Item> page2 = (PageResult<PageResult.Item>) codec.decode(method.getGenericReturnType(), json);

        System.out.println(page2);
        Assert.assertEquals(page.getResult().get(0).getAge(), page2.getResult().get(0).getAge());
    }
}